package com.example.controller.admin;


import com.example.dto.UserAddDTO;
import com.example.dto.UserPageQueryDTO;
import com.example.entity.PageBean;
import com.example.entity.User;
import com.example.result.PageResult;
import com.example.result.Result;
import com.example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j

public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/user/select")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "3") Integer pageSize) {
        log.info("分页查询：{}{}",page,pageSize );
        PageBean pageBean =userService.pageQuery(page,pageSize);
        return Result.success(pageBean);
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @GetMapping("/user/{id}")
    public Result  usersee(@PathVariable Integer id){
        log.info("根据id查询用户:{}",id);
        User user =  userService.see(id);
        return Result.success(user);
    }

//    @GetMapping("/selec")
//    public Result page1(UserPageQueryDTO userDTO) {
//        log.info("分页查询：{}",userDTO );
//        PageBean pageBean =userService.pageQuery1(userDTO);
//        return Result.success(pageBean);
//    }
    /**
     * 删除用户
     * @param id
     * @return
     */
    @DeleteMapping("/user/delete/{id}")
    public Result delet(@PathVariable Integer id){
            log.info("删除数据-controller");
            userService.dete(id);
        return Result.success("success");
    }

    /**
     * 添加数据
     * @param userDTO
     * @return
     */
    @PostMapping("/user/add")
    public Result add(@RequestBody UserAddDTO userDTO){
        log.info("添加数据:{}",userDTO);
        userService.add(userDTO);
        return Result.success();
    }
//    @PostMapping("/user/adds")
//    public Result adds(@RequestBody User user){
//        log.info("添加数据:{}",user);
//        userService.adds(user);
//        return Result.success();
//    }
    @PutMapping("/user/update")
    public Result update(@RequestBody UserAddDTO userDTO){
            log.info("更新员工:{}",userDTO);
            userService.update(userDTO);
            return Result.success();
    }

    @PostMapping("/conAdd")
    public Result conAdd(@RequestBody String conn){
        userService.conAdd(conn);
        return Result.success();
    }


}
